package com.ald.crossex.action.wechat;

import com.ald.crossex.dto.wechat.BaseResponse;
import com.ald.crossex.dto.wechat.OpenPlaceResponse;
import com.ald.crossex.dto.wechat.PublishJourneyRequest;
import com.ald.crossex.entity.TCeCoreUser;
import com.ald.crossex.entity.TCeCoreWechat;
import com.ald.crossex.service.UserService;
import com.ald.crossex.service.wechat.JourneyService;
import com.ald.crossex.service.wechat.WechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lishijiong on 15/3/11.
 */
@Controller
@RequestMapping(value = "/")
public class JourneyController extends BaseController {
    @Autowired
    private JourneyService journeyService;
    @Autowired
    private WechatService wechatService;
    @Autowired
    private UserService userService;


    @RequestMapping(value = "journey")
    public ModelAndView journeyRedirect(@RequestParam(value = "code") String code, @RequestParam(value = "state") String state, HttpServletRequest request) {

        TCeCoreWechat wechat = webAuthorize(wechatService, code, state);
        ModelAndView mav = new ModelAndView("journey");
        if(wechat!=null){
            mav.addObject("wid",wechat.getWid());
        }
        TCeCoreUser user = userService.getUserByWechatId(wechat.getWid());
        if(user != null){
            mav.addObject("uid",user.getUid());
        }
        generateJsSDKConfig(request, mav, code, state);
        return mav;
    }

    /**
     * 发布行程
     *
     * @param request
     * @return
     */
    @RequestMapping("journey/publish")
    @ResponseBody
    public BaseResponse publishJourney(@RequestBody PublishJourneyRequest request) {
        BaseResponse response = journeyService.publishJourney(request);
        return response;
    }

    /**
     * 获取开通的地区
     *
     * @return
     */
    @RequestMapping("journey/place")
    @ResponseBody
    public OpenPlaceResponse getOpenPlace() {
        OpenPlaceResponse response = journeyService.openPlace();
        return response;
    }


}
